﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_calculate_clicked()
{
    ui->lineEdit_result->setText("");
    QString strCalc = ui->lineEdit_expression->text();
    //删除空格
    strCalc.remove(QChar(' '), Qt::CaseInsensitive);
    std::string expression = strCalc.toStdString();

    bool isOk = false;
    QString faildReason = "";
    CalclulatorType::ExpressionJudge judge = calculator.checkExpression(expression);
    switch (judge) {
    case STR_EMPTY:
        faildReason = tr("输入表达式为空");
        break;
    case INPUT_ERR:
        faildReason = tr("输入的表达式有误");
        break;
    case MISS_MATCH:
        faildReason = tr("括号不配对");
        break;
    default:
        isOk = true;
        break;
    }
    if(isOk == false){
        QMessageBox::warning(this,tr("警告！"),faildReason);
        return;
    }
    bool dividendIsZero = false;
    QString result = QString::fromStdString(calculator.calcExpression(expression,dividendIsZero));
    ui->lineEdit_result->setText(result);
}

void MainWindow::on_pushButton_about_clicked()
{
    QString about = "简易表达式计算工具。\n";
    about += "  支持括号、正整数、负整数、浮点数计算\n";
    about += "  支持数学运算符：+ - * / %\n";
    about += "  支持逻辑运算符：&& || > ≥ < ≤ ==";

    QMessageBox::about(this,tr("关于"),about);
}

void MainWindow::on_pushButton_restart_clicked()
{
    ui->lineEdit_expression->setText("");
    ui->lineEdit_result->setText("");
}
